/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.PERSOclient.servlet;

import fr.annuaireComp.clientUtils.CompetencePersonnelModel;
import fr.PERSOclient.models.ProfileCompModel;
import fr.PERSOclient.utils.AuthManager;
import fr.annuaireComp.clientUtils.CompetenceModel;
import fr.annuaireComp.clientUtils.ProfileModel;
import fr.annuaireComp.entities.Competence;
import fr.annuaireComp.entities.CompetencePersonnel;
import fr.annuaireComp.entities.Personnel;
import fr.annuaireComp.sessions.CompetencesManager;
import fr.annuaireComp.sessions.CompetencePersonnelManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author MiniK
 */
@WebServlet(name = "profilecomp", urlPatterns = {"/profilecomp"})
public class profilecomp extends HttpServlet {

    @EJB
    private CompetencePersonnelManager persCompMan;
    @EJB
    private CompetencesManager compMan;

    //@EJB
    //private PersonnelManager persMan;
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if (!AuthManager.logUser(request, response)) {
            return;
        }


        if ("compinfos".equals(request.getParameter("action"))) {
            String id = request.getParameter("id");
            Competence comp = compMan.getCompetence(Integer.parseInt(id));
            CompetenceModel compdata = CompetenceModel.buildCompetenceModel(comp);
            request.setAttribute("compinfosdata", compdata);
            request.getRequestDispatcher("/WEB-INF/p_compinfos.jsp").forward(request, response);
        } else {
            Personnel pers = AuthManager.getCurrentUser(request);
            //   pers = persMan.getPersonnel(pers.getIdpersonnel());

            List<Competence> compList = compMan.listCompetences();
            //  List<CompetenceModel> userComp = new ArrayList<Competence>();

            ProfileCompModel profilecompdata = new ProfileCompModel();
            profilecompdata.currentUser = ProfileModel.buildProfileModel(pers);
            profilecompdata.competenceTree = CompetenceModel.buildCompetenceModelFromList(compList);
            profilecompdata.userCompetences = new ArrayList<CompetencePersonnelModel>();

            pers.setCompetencePersonnelList(persCompMan.listCompetencePersonnelOf(pers));
            for (CompetencePersonnel cp : pers.getCompetencePersonnelList()) {
                profilecompdata.userCompetences.add(new CompetencePersonnelModel(cp.getCommentaire(), cp.getAnneesXp().toString(),
                        CompetenceModel.buildCompetenceModel(cp.getCompetence())));
            }

            request.setAttribute("profilecompdata", profilecompdata);

            request.getRequestDispatcher("/WEB-INF/profilecomp.jsp").forward(request, response);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("action");

        if ("edit".equals(action)) {
            String persCompAttr = request.getParameter("attribute").trim();
            String persCompId = request.getParameter("element_id").trim();
            String update_value = request.getParameter("update_value").trim();
            String original_html = request.getParameter("original_html").trim();
            String returnedValue = update_value;
            try {
                CompetencePersonnel cp = new CompetencePersonnel(Integer.parseInt(persCompId), AuthManager.getCurrentUser(request).getIdpersonnel());
                if ("commentaire".equals(persCompAttr)) {
                    cp.setCommentaire(update_value);
                }
                if ("anneesxp".equals(persCompAttr)) {
                    cp.setAnneesXp(Integer.parseInt(update_value));
                }

                persCompMan.editPersonnelCompetence(cp);
            } catch (Exception ex) {
                returnedValue = original_html;
            }
            response.getWriter().print(returnedValue);
        }

        if ("add".equals(action)) {
            Integer userId = AuthManager.getCurrentUser(request).getIdpersonnel();
            Integer compId = Integer.parseInt(request.getParameter("compid"));
            String commentaire = request.getParameter("commentaire");
            Integer anneeXp = Integer.parseInt(request.getParameter("anneexp"));
            /*
            Personnel p = AuthManager.getCurrentUser(request);
            CompetencePersonnel cp = new CompetencePersonnel(compId, userId);
            cp.setAnneesXp(anneeXp);
            cp.setCommentaire(commentaire);
            p.getCompetencePersonnelList().add(cp);
            
            persMan.editPersonnel(p);
             */
            try {
                persCompMan.addPersonnelCompetence(userId, compId, anneeXp, commentaire);
            } catch (Exception ex) {
                String e = ex.getMessage();
            }
            response.sendRedirect("profilecomp");
        }

        if ("delete".equals(action)) {
            Integer userId = AuthManager.getCurrentUser(request).getIdpersonnel();
            Integer compId = Integer.parseInt(request.getParameter("id"));
            persCompMan.deletePersonnelCompetence(userId, compId);
            response.getWriter().print("");
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
